package org.jbit.buy.daoimpl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.jbit.buy.dao.BaseDao;
import org.jbit.buy.dao.easybuy_newsDao;
import org.jbit.buy.entity.easybuy_news;



public class easybuy_newsImpl  extends BaseDao implements easybuy_newsDao{

	@Override
	public List<easybuy_news> findAllNews() throws SQLException {
		 List<easybuy_news> list = new ArrayList<easybuy_news>();
	        ResultSet rs = null;
//	        String sql = "SELECT * from news,topic where news.ntid =topic.tid order by ntid,ncreatedate desc limit 30";
	        String sql = "SELECT * from easybuy_news  order by createTime desc ";
	        try {
	            rs = this.exxcuteQuery(sql);
	            easybuy_news news = null;
	            while (rs.next()) {
	                news = new easybuy_news();
	                news.setId(rs.getInt("id"));
	                news.setTitle(rs.getString("title"));
	                news.setContent(rs.getString("content"));
	                news.setCreateTime(rs.getString("createTime"));
	                list.add(news);
	            }
	        } catch (SQLException e) {
	            e.printStackTrace();
	           
	        } finally{
	        	closeAll(connection, pstmt, rs); 	
	        }
	        return list;
	}
	public List<easybuy_news> getAllNewsByPage(int pageNo, int pageSize) throws SQLException {
        ResultSet rs = null;
        String sql = "SELECT * from easybuy_news  order by createTime desc limit ?,?";
        List<easybuy_news> newList=new ArrayList<easybuy_news>();
      
        try {
            rs = this.exxcuteQuery(sql,(pageNo-1)*pageSize,pageSize);
            easybuy_news  enews = null;
            while (rs.next()) {
                enews = new easybuy_news();
                enews.setId(rs.getInt("id"));
                enews.setTitle(rs.getString("title"));
                enews.setContent(rs.getString("content"));
                enews.setCreateTime(rs.getString("createTime"));
                newList.add(enews);
            }
        } catch (SQLException e) {
            e.printStackTrace();
           
        } finally{
        	closeAll(connection, pstmt, rs); 	
        }
        return newList;
	}
	@Override
	public int getTotslCount() {
		  ResultSet rs = null;
	        String sql = "SELECT COUNT(1) FROM `easybuy_news` ";
	        int count = -1;
	        try {
	            rs = this.exxcuteQuery(sql);
	            rs.next();
	            count = rs.getInt(1);
	        } catch (SQLException e) {
	            e.printStackTrace();
	            
	        }finally{
	        	closeAll(connection, pstmt, rs); 		
	        return count;
	}}

}
